<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommonController
{
    public function user_list() {
        session('page_size',$_GET['page_size']);
        $search = $_GET['user_name'];
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M("user");
        $where['username'] = array('like','%'.$search.'%');
        $where['del'] = '0';
        $data=$MODEL->page($page_no, $page_size)->order('id desc')->where($where)->select();
        $count = $MODEL->where($where)->count();
        //总记录数
        $Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->assign('type',$_SESSION['type']);
        //dump($a);exit();
        $this->display();
    }
    //搜索
    public function do_search(){
        $search = $_GET['user_name'];
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 10;
        $MODEL = M("user");
        $where['user_name'] = array('like','%'.$search.'%');
        $data=$MODEL->page($page_no, $page_size)->order('id desc')->where($where)->select();
        $count = $MODEL->where($where)->count();
        //总记录数
        $Page = new \AntAge\Page($count, $page_size);
        // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        // 分页显示输出
        $this->assign('page', $show);
        // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->assign('type',$_SESSION['type']);
        //dump($a);exit();
        $this->display();
    }

    public function add() {
        if (I('post.mobile') == "" && I('post.password') == "") {
           
            $this->assign('type',$_SESSION['type']);
            $this->display();
        }else {
            if($_SESSION['type'] == 0){
                $this->error('你没有权限');
            }
            $MODEL = D("User");
            if(!$_POST['username']){
                $this->error('用户名不能为空');
            }
            if(!$_POST['password']){
                 $this->error('密码不能为空');
            }
            if(!$_POST['phone']){
                 $this->error('电话不能为空');
            }

            $username = M('user')->where(array('username'=>$_POST['username']))->find();

            if($username){
                $this->error('该用户名已经存在');
            }
            if(!$MODEL->create()){
                exit($MODEL->getError());
            }else {
                if (!is_numeric($_POST['phone']) || strlen($_POST['phone']) != 11) {
                    $this->error('请输入正确的手机号');
                }
                $where['username']=I('post.username');
                $where['del']='0';
                $mobile=$MODEL->where($where)->find();
                if ($mobile) {
                    $this->error('该账号已注册');
                }
                $MODEL->password=md5(I('post.password'));
                $temp = $MODEL->add();

                if ($temp) {
                    $this->success('数据添加成功！', 'User_list');
                }else {
                    $this->error('数据未插入');
                }
            }
        }
    }

    public function updata() {
        $id = $_GET['id'];
        $MODEL = M("user");
        $data = $MODEL->field('id,username,zs_name,phone,weixin_name')->find($id);
        $this->assign("shop", $data);
        $this->assign('type',$_SESSION['type']);
        $this->assign("data", $data);
        $this->display();
    }

    public function do_updata() {

        $MODEL = M('user');
        if(!$_POST['username']){
             $this->error('用户名不能为空');
        }
        if(!$_POST['phone']){
             $this->error('电话不能为空');
        }
        $id = M('user')->where(array('username'=>$_POST['username']))->getField('id');
        if($id != $_POST['id']){
            $this->error('用户名重复');
        }
        $MODEL->create();
        if(empty($_POST['password'])){
            $data = array(
                'username' => $_POST['username'],
                'zs_name'   => $_POST['zs_name'],
                'phone'     => $_POST['phone'],
                'weixin_name' => $_POST['weixin_name']
            );
        }else{
            $data = array(
                'username' => $_POST['username'],
                'zs_name'   => $_POST['zs_name'],
                'phone'     => $_POST['phone'],
                'weixin_name' => $_POST['weixin_name'],
                'password'  => md5($_POST['password'])
            );
        }
        $temp1 = M('user')->where('id='.$_POST['id'])->save($data);

        if ($temp1===false) {
            $this->error('数据未插入');
        }
        else {
            $this->success('数据修改成功！', 'User_list');
        }
    }

    public function delete() {
        if($_SESSION['type'] == 0){
                $this->error('你没有权限');
            }
        $id['id'] = I('post.id');
        $data = M('user')->where($id)->delete();
        if ($data) {
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    public function money_log_z(){
        $page_no = I('get.p') ? I('get.p') : 1;
        $page_size = $_GET['page_size'] ? $_GET['page_size'] : 15;
        $MODEL = D("TransfersLog");
        $data = $MODEL->page($page_no, $page_size)->order('id desc')->where('log_type=0')->select();
        $count = $MODEL->where('log_type=0')->count();
         //总记录数
        $Page = new \AntAge\Page($count, $page_size);
         // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
         // 分页显示输出
        $this->assign('page', $show);
         // 赋值分页输出
        $this->assign("count", $count);
        $this->assign("data", $data);
        $this->display();
    }

}


